package com.example.zcq.rxjava2retrofitmvp.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.zcq.rxjava2retrofitmvp.R;
import com.example.zcq.rxjava2retrofitmvp.util.DensityUtil;

/**
 * @author zcq
 * @package com.example.zcq.rxjava2retrofitmvp.view
 * @fileName Rxjava2RetrofitMVP
 * @description: 这个类的描述
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/9/27  21:05
 */
public class CustomDialog extends Dialog {
    private Context context;
    private int height, width;
    private boolean cancelTouchout;
    private View view;

    private CustomDialog(Builder builder) {
        super(builder.context);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

    private CustomDialog(Builder builder, int resStyle) {
        //等会生成的Dialog就会带有这个R.style.XXX所设置的效果。
        super(builder.context, resStyle);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局
        setContentView(view);
        //点击别处是否消失
        setCancelable(cancelTouchout);
        //设置布局长宽
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = width;
        layoutParams.height = height;
        window.setAttributes(layoutParams);
    }

    /**
     * 我这边的Builder中对height和width写了三种方式，
     * 比如直接写入px的值就调用heightpx(),如果直接写入dp值，就调用heightdp()。
     * 不过最多的应该还是调用heightDimenRes()方法。因为一般我们在写自定义layout布局的时候，height和width的数值肯定是去dimen.xml中获取。
     * 所以我们在代码中生成这个自定义对话框的时候,也就直接调用了heightDimenRes(R.dimen.XXX)。这样。
     * 我们什么时候需求变了，说这个对话框的大小要进行更改，我们不需要更改代码，只需要在demen.xml中将数值修改即可。
     */

    public static final class Builder {
        private Context context;
        private int height, width;
        private boolean cancelTouchout;
        private View view;
        private int resStyle = -1;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder view(int resView) {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }

        public Builder heightpx(int val) {
            height = val;
            return this;
        }

        public Builder widthpx(int val) {
            width = val;
            return this;
        }

        public Builder heightdp(int val) {
            height = DensityUtil.dp2px(context, val);
            return this;
        }

        public Builder widthdp(int val) {
            width = DensityUtil.dp2px(context, val);
            return this;
        }

        public Builder heightDimenRes(int dimenRes) {
            height = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder widthDimenRes(int dimenRes) {
            width = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnclick(int viewRes, View.OnClickListener listener) {
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }

        public CustomDialog build() {
            if (resStyle != -1) {
                return new CustomDialog(this, resStyle);
            } else {
                return new CustomDialog(this);
            }
        }
    }

    /*示例1
    CustomDialog.Builder builder = new CustomDialog.Builder(this);
    dialog =builder.cancelTouchout(false).

    view(R.layout.dialog_loginerror)            .

    heightDimenRes(R.dimen.dialog_loginerror_height)            .

    widthDimenRes(R.dimen.dialog_loginerror_width)            .

    style(R.style.Dialog)            .

    addViewOnclick(R.id.btn_cancel,new View.OnClickListener() {
        @Override public void onClick (View view){
            dialog.dismiss();
        }
    }).

    build();dialog.show();*/

/*示例2
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.jbperson:
                    Toast.makeText(aty, "选择经办人按钮", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.spperson:
                    Toast.makeText(aty, "选择审批人按钮", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.confirmbtn:
                    Toast.makeText(aty, "点击确定按钮", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    CustomDialog.Builder builder = new CustomDialog.Builder(this);
    CustomDialog dialog = builder.style(R.style.Dialog)
            .heightDimenRes(R.dimen.dilog_identitychange_height)
            .widthDimenRes(R.dimen.dilog_identitychange_width).cancelTouchout(false)
            .view(R.layout.dialog_identitychange).addViewOnclick(R.id.jbperson, listener)
            .addViewOnclick(R.id.spperson, listener).addViewOnclick(R.id.confirmbtn, listener)
            .build();

            dialog.show();
*/

}
